/*
 * Copyright (c) 2019-2029, <a href="https://wwww.intellijboy.cn">https://wwww.intellijboy.cn</a> (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * <a href="https://www.apache.org/licenses/LICENSE-2.0">https://www.apache.org/licenses/LICENSE-2.0</a>
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package client.intellijboy.demo;

import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 会话测试
 *
 * @author intellijboy
 * @date 2023/12/8
 * @since 1.0
 */
@Tag(name = "会话测试模块", description = "测试Session功能特性")
@RestController
@RequestMapping("session")
@Slf4j
public class SessionDemoContoller {

    @GetMapping("/set-cookie")
    public String setCookie(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        log.warn("会话ID：{}", session.getId());
        // 创建一个Cookie对象
        Cookie cookie = new Cookie("intellijboy", "your_session_id");
        // 设置Cookie的属性
        int maxInactiveInterval = session.getMaxInactiveInterval();
        log.warn("会话超时时间：{}", maxInactiveInterval);
        cookie.setMaxAge(5); // 设置Cookie的过期时间，单位为秒
        cookie.setPath("/"); // 设置Cookie的作用路径，根路径表示对整个网站有效
        // 将Cookie添加到响应中
        response.addCookie(cookie);
        return "Cookie set successfully";
    }
}
